package pw.goyd.game.handler.imp;

import pw.goyd.game.api.DBDone;
import pw.goyd.game.api.Utils;
import pw.goyd.game.constant.setting.GameSetting;
import pw.goyd.game.handler.Handler;
import pw.goyd.game.logic.AdAwardRequest;
import pw.goyd.game.logic.AdAwardResponse;
import pw.goyd.game.server.Log;
import pw.goyd.game.server.LoginList;
import pw.goyd.protocol.model.Response;
import pw.goyd.subscribe.PlayerCustomer;

/**
 * Created by StanWind on 2016/11/4.
 */
public class ADAWARD extends Handler implements GameSetting {
    @Override
    public void handle() {
        PlayerCustomer p = LoginList.getPlayer(ctx.getChannel().getId());
        if (p == null) {
            Log.getLogger().error("空指针--->ChannelID[" + ctx.getChannel().getId() + "]");
            return;
        }
        AdAwardRequest adAwardRequest = new AdAwardRequest();
        adAwardRequest.readFromBytes(getRequest().getData());
        //是否发放今日奖励
        if (!Utils.getCurrentDay().equals(p.getLast_lucky_time())) {
            p.setLucky_count(BAISC_LUCKY_COUNT);
            p.setLast_lucky_time(Utils.getCurrentDay());
        }

        AdAwardResponse adAwardResponse = new AdAwardResponse();
        if (adAwardRequest.getState() == 0) {                                 //   state   获取信息
            adAwardResponse.setState(0);
            adAwardResponse.setCurrent(p.getLucky_count());
        } else {                                                              //   state    广告奖励
            //先判断上次是不是当天 是当天 那么次数有效 不是当天 次数无效
            if (Utils.getCurrentDay().equals(p.getLast_ad_time())) {
                //次数有效 判断次数
                if (p.getAd_count() < MAX_AD_COUNT) {           //  当天奖励没满
                    p.setAd_count(p.getAd_count() + 1);         //  增加广告激励次数
                    p.setLucky_count(p.getLucky_count() + 1);   //  增加抽奖次数
                    adAwardResponse.setState(0);                //  回写状态成功
                } else {
                    adAwardResponse.setState(1);                //  回写状态失败  已满次数
                    Log.getLogger().debug(p.getNickname() + "--->广告激励已达" + MAX_AD_COUNT + "次");
                }
            } else {
                //次数无效
                p.setLast_ad_time(Utils.getCurrentDay());   //  设置今日时间
                p.setAd_count(1);                           //  设置激励次数 =  1
                p.setLucky_count(p.getLucky_count() + 1);   //  增加抽奖次数
                adAwardResponse.setState(0);                //  回写状态成功
            }
            DBDone.updateLucky(p);                          //  存储

            adAwardResponse.setCurrent(p.getLucky_count());
        }

        Log.getLogger().debug("Award---->" + p.getNickname() + "-" + p.getLast_lucky_time() + "-" + p.getLucky_count()
                + "-" + p.getLast_ad_time() + "-" + p.getAd_count());
        Response response = new Response();

        response.setCmd(CMD_ADAWARD);
        response.setData(adAwardResponse.getBytes());
        response.setTime(0);
        this.getCtx().getChannel().write(response);
    }
}
